package org.esk.dablog.web.forms;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.validation.BindException;
import org.esk.dablog.service.PictureManager;
import org.esk.dablog.model.Picture;
import org.esk.dablog.web.util.RequestUtils;
import org.esk.dablog.web.functions.EntryFunctions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class is used to edit Picture's properties
 * User: jc
 * Date: 30.11.2006
 * Time: 11:59:49
 * $Id:$
 */
public class DeletePictureForm extends ParameterizableViewController {
    private PictureManager pictureManager;


    /**
     *
     */
    public DeletePictureForm() {
    }


    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        long id = RequestUtils.extractLongParameter(request);
        Picture p = pictureManager.getPicture(id);
        RedirectView view = new RedirectView("/" + getViewName() + "/" + p.getEntry().getId(), true);
        if (EntryFunctions.isPictureOwner(p, request.getRemoteUser())) {
            pictureManager.deletePicture(p);
        }
        return new ModelAndView(view);
    }

    public void setPictureManager(PictureManager pictureManager) {
        this.pictureManager = pictureManager;
    }
}
